export default {
  methods: {
    /**
     * 商品数量修改操作
     */
    addGoodsChange(selectNumber, rowData) {
      selectNumber = Number(selectNumber)

      rowData.selectNumber = selectNumber

      let { allCarts, currentRoomShopCart, currentShopCart } = this.getCurrentRoomShopCart()

      // console.log('>>>', allCarts, currentRoomShopCart, currentShopCart)

      const ifInitRoomShopCartInStorage = currentRoomShopCart ? true : false

      /**
       * 当前房台购物车对象不存在
       * currentRoomShopCart: { roomid: '001', shopCart: [{'name': '商品1'}, { name: '商品2'}]}
       * 1. 初始化购物车对象的购物车数组
       * 2. 直接往购物车数组追加该商品信息
       */
      if (!currentShopCart) {
        currentRoomShopCart = { ...this.loginInfo }
        currentShopCart = []
        // rowData.selectNumber = selectNumber
        rowData.total = this.$utils.mathMultiply(selectNumber, rowData.Price)
        currentShopCart.push(rowData)
      }

      // 购物车内有东西了
      if (currentShopCart) {
        const ifExistedGood = this.ifThisGoodExistedInCurrentShopCart(currentShopCart, rowData)
        console.log('ifExistedGood', ifExistedGood)

        // 如果本次操作的商品购物车不存在，证明没添加过。 直接追加进购物车
        if (!ifExistedGood) {
          // rowData.selectNumber = selectNumber
          rowData.total = this.$utils.mathMultiply(selectNumber, rowData.Price)
          currentShopCart.push(rowData)
        }

        // 如果购物车存在该商品，判断用户操作是修改 还是 移除
        if (ifExistedGood) {
          ifExistedGood.selectNumber = selectNumber
          ifExistedGood.total = rowData.total = this.$utils.mathMultiply(selectNumber, rowData.Price)

          // 如果是套餐，还需要同步更新下组商品的选中数据
          if (this.isMeal(rowData.set) && rowData.mealForm) {
            ifExistedGood.mealForm = rowData.mealForm
          }
        }
      }

      // 重新计算总商品数量和总价
      let totalPrice = 0
      let totalNumber = 0

      for (let i in currentShopCart) {
        totalPrice = totalPrice + currentShopCart[i].total
        totalNumber = totalNumber + currentShopCart[i].selectNumber
      }

      currentRoomShopCart.totalPrice = totalPrice
      currentRoomShopCart.totalNumber = totalNumber

      // 初始化所有购物车商品的例送数据
      this.currentShopCart = currentShopCart = this.computedCartLsNumber(currentShopCart)

      // 如果本房台购物车数据未在缓存中找到，添加到缓存
      currentRoomShopCart.shopCart = currentShopCart
      if (!ifInitRoomShopCartInStorage) {
        allCarts.push(currentRoomShopCart)
      }

      // 购物车商品数据缓存至本地
      uni.setStorageSync(this.shopCartStorageName, allCarts)

      /**
       * 保证数据同步更新
       * 如果是购物车操作添加移除，需要充值给 列表的数据（goodList, labalNumber ..）更新 。
       */
      setTimeout(() => {
        if (this.$refs.searchModal && this.$refs.searchModal.state()) {
          this.$refs.searchModal.init(this.$refs.searchModal.goodsList, true)
        }

        this.init()
      })
    },

    // 判断当前操作商品是否存在于购物车
    ifThisGoodExistedInCurrentShopCart(currentShopCart, rowData) {
      for (var i in currentShopCart) {
        if (currentShopCart[i].id == rowData.id) {
          return currentShopCart[i]
        }
      }

      return null
    },

    computedCartLsNumber(currentShopCart) {
      if (!currentShopCart || currentShopCart.length === 0) return currentShopCart

      // 计算例送数量 ， 如果有例送更新到 rowData.lscsNumber
      currentShopCart.forEach(rowData => {
        const lsNumber = this.getLscsNumber(rowData.selectNumber, rowData.Lscs)
        rowData.lsNumber = lsNumber
      })

      return currentShopCart
    },

    // 显示购物车
    showShoppingCar() {
      if (this.goodsTotalNumber == 0) {
        return
      }
      this.footHeight = '100%'
      this.showCar = true
      this.carGoodsScrollTop = 0
    },

    // 隐藏购物车
    hideShoppingCar() {
      this.footHeight = '0'
      this.showCar = false
    },

    // 清空该门店的购物车
    clearShopCart() {
      let { allCarts, currentRoomShopCart, currentShopCart } = this.getCurrentRoomShopCart()

      // 购物车信息初始化
      this.currentShopCart = currentRoomShopCart.shopCart = []

      currentRoomShopCart.totalPrice = 0
      currentRoomShopCart.totalNumber = 0
      this.currentRoomShopCart = currentRoomShopCart

      uni.setStorageSync(this.shopCartStorageName, allCarts)

      // 渲染的列表信息初始化
      for (let i in this.goodsList) {
        this.goodsList[i].selectNumber = 0
      }

      for (let i in this.categoryList) {
        this.categoryList[i].selectNumber = 0
      }

      // 隐藏购物车
      this.hideShoppingCar()
    }
  }
}
